﻿using System;
using Pathfinding;
using UnityEngine;
using XLua;
using Yoozoo.Gameplay.Liberty.PlayerController;
using Yoozoo.Gta.Common;

namespace Yoozoo.Gameplay.Liberty.AI
{
    public static class LibertyAIUtils
    {
        public const int PLAYER_AI_ID = -1;
        
        public static LibertyManager LibertyManager;

        public static LibertyAIManager AIManager
        {
            get
            {
                if (LibertyManager == null)
                {
                    return null;
                }
                return LibertyManager.AIManager;
            }
        } 

        public static LibertyTrafficManager TrafficManager;
        public static LibertyPathManager PathManager => TrafficManager.PathManager;

        public static PlayerControlModeMgr PlayerController;

        public static Camera PlayerCamera { get; set; }


        #region 相关回调

        public static Action<int,NpcStatus> OnResetDefaultStatus;

        #endregion

        public static void StaticNpcSwitchNavigation(int npcId,bool enable)
        {
            
            
        }

        // 是否是玩家触发碰撞体
        public static bool IsPlayerTriggerCollider(Collider col)
        {
            return col == PlayerController.characterCtrl || col == PlayerController.playerCollider;
        }

        public static string GetGraphResPath(int graphId)
        {
            return AIManager.GetGraphResPath(graphId);
        }

        public static bool AddNpc(AIBirthData birthData)
        {
            if (birthData.isStatic)
            {
                return AIManager.AddNpc(birthData);
            }
            else
            {
                return AIManager.DynamicNpcManager.AddDynamicNpc(birthData);
            }
        }

        public static AIEntity GetNpc(int uniqueId)
        {
            if (AIManager == null || AIManager.NpcBirthManager == null)
            {
                return null;
            }
            return AIManager.NpcBirthManager.GetNpc(uniqueId);
        }

        public static AIEntity GetPlayerEntity()
        {
            if (PlayerController)
            {
                return PlayerController.playerEntity.AIEntity;
            }
            return GetNpc(PLAYER_AI_ID);
        }
        
        public static LuaTable GetNpcCfg(string rootName)
        {
            return ResidentHandler.Inst.GetFunction("CityLuaFunction.getNpcCfg").Func<string, LuaTable>(rootName);
        }
        
        public static int GetGlobalValueCfg(string key)
        {
            return ResidentHandler.Inst.GetFunction("CityLuaFunction.getGlobalValueCfg").Func<string, int>(key);
        }

        public static GraphNode GetPointOnNavmesh(Vector3 position, NNConstraint constraint = null)
        {
            var hangingGraph = AstarPath.active.data.FindGraphByGraphName("sidewalk_road");
            if (hangingGraph == null)
            {
                return null;
            }

            RecastGraph navmeshBase = hangingGraph as RecastGraph;

            return navmeshBase.PointOnNavmesh(position, constraint);
        }
    }
}